﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace PixelImageCalibrator
{
    public partial class MainForm : Form
    {
        private Bitmap sourceImage;
        private float zoom = 1.0f;
        private PointF panOffset = PointF.Empty;
        private Point lastMousePos;
        private bool isPanning;

        public MainForm()
        {
            InitializeComponent();
            DoubleBuffered = true;
            this.MouseWheel += MainForm_MouseWheel;
        }

        // 初始化组件
        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.ClientSize = new Size(800, 600);
            this.Text = "Pixel Image Calibrator";
            this.ResumeLayout(false);

            // 添加菜单
            var menu = new MenuStrip();
            var fileMenu = new ToolStripMenuItem("文件");
            fileMenu.DropDownItems.Add("打开...", null, (s, e) => OpenImage());
            menu.Items.Add(fileMenu);
            this.Controls.Add(menu);

        }

        // 打开图像文件
        private void OpenImage()
        {
            using (var ofd = new OpenFileDialog())
            {
                ofd.Filter = "Image Files|*.bmp;*.png;*.jpg;*.jpeg";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    sourceImage?.Dispose();
                    sourceImage = new Bitmap(ofd.FileName);
                    panOffset = PointF.Empty;
                    zoom = 1.0f;
                    Invalidate();
                }
            }
        }

        // 鼠标滚轮缩放
        private void MainForm_MouseWheel(object sender, MouseEventArgs e)
        {
            var zoomFactor = 1.1f;
            var oldZoom = zoom;

            if (e.Delta > 0)
                zoom *= zoomFactor;
            else
                zoom /= zoomFactor;

            zoom = Math.Max(0.1f, Math.Min(zoom, 20f));

            // 保持鼠标位置为缩放中心
            var imagePos = ScreenToImage(e.Location);
            panOffset.X += (imagePos.X * oldZoom - imagePos.X * zoom);
            panOffset.Y += (imagePos.Y * oldZoom - imagePos.Y * zoom);

            Invalidate();
        }

        // 鼠标事件处理
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isPanning = true;
                lastMousePos = e.Location;
            }
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            isPanning = false;
            base.OnMouseUp(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (sourceImage != null)
            {
                var imgPos = ScreenToImage(e.Location);
                Text = $"X: {(int)imgPos.X}, Y: {(int)imgPos.Y}";
            }
            if (isPanning && sourceImage != null)
            {
                var delta = new Point(e.X - lastMousePos.X, e.Y - lastMousePos.Y);
                panOffset.X += delta.X;
                panOffset.Y += delta.Y;
                lastMousePos = e.Location;
                Invalidate();
            }
            base.OnMouseMove(e);
        }

        // 坐标转换
        private PointF ScreenToImage(Point screenPoint)
        {
            return new PointF(
                (screenPoint.X - panOffset.X) / zoom,
                (screenPoint.Y - panOffset.Y) / zoom);
        }

        // 主绘制方法
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (sourceImage == null) return;

            e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
            e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;

            // 绘制图像
            var destRect = new RectangleF(
                panOffset.X,
                panOffset.Y,
                sourceImage.Width * zoom,
                sourceImage.Height * zoom);

            e.Graphics.DrawImage(sourceImage, destRect,
                new Rectangle(0, 0, sourceImage.Width, sourceImage.Height),
                GraphicsUnit.Pixel);

            // 绘制网格
            if (zoom > 5)
            {
                using (var gridPen = new Pen(Color.FromArgb(50, Color.Green)))
                {
                    for (int x = 0; x <= sourceImage.Width; x++)
                    {
                        float screenX = panOffset.X + x * zoom;
                        e.Graphics.DrawLine(gridPen,
                            screenX, panOffset.Y,
                            screenX, panOffset.Y + sourceImage.Height * zoom);
                    }

                    for (int y = 0; y <= sourceImage.Height; y++)
                    {
                        float screenY = panOffset.Y + y * zoom;
                        e.Graphics.DrawLine(gridPen,
                            panOffset.X, screenY,
                            panOffset.X + sourceImage.Width * zoom, screenY);
                    }
                }
            }
        }
    }
}